# app/models.py

from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from .database import Base

class WebhookEvent(Base):
    __tablename__ = 'webhook_events'

    id = Column(Integer, primary_key=True, index=True)
    repository_name = Column(String(255), nullable=False)
    repository_owner = Column(String(255), nullable=False)
    pusher = Column(String(255), nullable=False)
    ref = Column(String(255), nullable=False)
    before_sha = Column(String(40), nullable=False)
    after_sha = Column(String(40), nullable=False)
    received_at = Column(DateTime, default=datetime.utcnow)

    invalid_commits = relationship('InvalidCommit', back_populates='event')

    def __repr__(self):
        return f"<WebhookEvent(id={self.id}, repository={self.repository_owner}/{self.repository_name})>"

class InvalidCommit(Base):
    __tablename__ = 'invalid_commits'

    id = Column(Integer, primary_key=True, index=True)
    sha = Column(String(40), nullable=False)
    author = Column(String(255), nullable=False)
    message = Column(Text, nullable=False)
    url = Column(String(255), nullable=False)
    detected_at = Column(DateTime, default=datetime.utcnow)
    event_id = Column(Integer, ForeignKey('webhook_events.id'), nullable=False)

    event = relationship('WebhookEvent', back_populates='invalid_commits')

    def __repr__(self):
        return f"<InvalidCommit(sha={self.sha}, author={self.author})>"
